<?php
// +----------------------------------------------------------------------
// | @filename MenuTransformer.php
// +----------------------------------------------------------------------
// | @author 落花轻舞问君愁 <mwcx1990@qq.com>
// +----------------------------------------------------------------------
// | @datetime 2021-11-01 18:00:05
// +----------------------------------------------------------------------
// | @Description
// +----------------------------------------------------------------------

namespace Addons\Backend\Transformers;

use Addons\Backend\Models\Menu;

class MenuTransformer extends BaseTransformer
{
    protected $availableIncludes = ['roles'];

    public function transform(Menu $menu)
    {
        return [
            'id' => $menu->id,
            'hidden' => $menu->hidden,
            'title' => $menu->title,
            'pid' => $menu->pid,
            'icon' => (string)$menu->icon,
            'path' => $menu->path,
            'name' => $menu->name,
            'redirect' => (string)$menu->redirect,
            'breadcrumb' => $menu->breadcrumb,
            'no_cache' => $menu->no_cache,
            'affix' => $menu->affix,
            'sort' => $menu->sort
        ];
    }

    public function includeRoles(Menu $menu)
    {
        return $this->item($menu->roles, function ($data) {
            return array_column($data->toArray(), 'id');
        }, '');
    }
}
